//
//  CommonTableViewCell.m
//  Fragrance
//
//  Created by dllo on 15/5/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CommonTableViewCell.h"
#import "DataBaseManager.h"

@implementation CommonTableViewCell

- (void)dealloc
{
    [_goodsIcon release];
    [_chefIcon release];
    [_nameLabel release];
    [_shopNameLabel release];
    [_descLabel release];
    [_MyScrollView release];
    [_model release];
    [_price release];
    [super dealloc];
    
    
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self CreateCell];
    }
    return  self;
    
}
- (void)CreateCell
{
    self.chefIcon = [[[UIImageView alloc] init] autorelease];
    [self.contentView addSubview:self.chefIcon];
    
    self.nameLabel = [[[UILabel alloc] init] autorelease ];
    [self.contentView addSubview:self.nameLabel];
    
    self.shopNameLabel = [[[UILabel alloc] init] autorelease];
    [self.contentView addSubview:self.shopNameLabel];
    
    
    self.descLabel = [[[UILabel alloc] init] autorelease];
    [self.contentView addSubview:self.descLabel];
    
    self.favoriteNumButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.favoriteNumButton addTarget:self action:@selector(collecting) forControlEvents:UIControlEventTouchUpInside];
    [self.favoriteNumButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    [self.contentView addSubview:self.favoriteNumButton];
    
    self.MyScrollView = [[[UIScrollView alloc] init] autorelease];
    self.MyScrollView.showsVerticalScrollIndicator = NO;
    self.MyScrollView.showsHorizontalScrollIndicator = NO;
    self.MyScrollView.pagingEnabled = YES;
    self.MyScrollView.bounces = NO;
    self.MyScrollView.delegate = self;
    [self.contentView addSubview:self.MyScrollView];
    
    self.price = [[[UILabel alloc] init] autorelease];
    [self.contentView addSubview:self.price];
    
    
}
-  (void)layoutSubviews
{
    [super layoutSubviews];
    self.MyScrollView.frame = CGRectMake(10, 10, self.frame.size.width  , 150);
    self.chefIcon.frame = CGRectMake(30, 150, 50, 50);
    self.chefIcon.layer.masksToBounds = YES;
    self.chefIcon.layer.cornerRadius = 25;
    self.nameLabel.frame = CGRectMake(100, 160, 220, 50);
    self.shopNameLabel.frame = CGRectMake(30, 200, 150, 40);
     self.favoriteNumButton.frame = CGRectMake(200, 200, 100, 40);
    self.descLabel.frame = CGRectMake(10, 240, 310, 100);
    self.descLabel.numberOfLines = 3;
    [self.descLabel sizeToFit];
   
    
    
    
    
}
- (void)setModel:(CftModelData *)model
{
    
    self.nameLabel.text = model.name;
    self.shopNameLabel.text = model.shopName;
    self.descLabel.text = model.desc;
    [self.favoriteNumButton setTitle:model.favoriteNum forState:UIControlStateNormal];
    BOOL result = [DataBaseManager isExists:model.name];
    if (!result) {
        [self.favoriteNumButton setImage:[UIImage imageNamed:@"quxiaoshoucang1"] forState:UIControlStateNormal];
    } else {
        [self.favoriteNumButton setImage:[UIImage imageNamed:@"shoucang1"] forState:UIControlStateNormal];
    }

    [self.chefIcon setImageWithURL:[NSURL URLWithString:model.chefIconUrl] placeholderImage:nil];
    
    
    for (int i = 0 ; i < model.picListarrayData.count; i++) {
        PicListModelData *model1 = [model.picListarrayData objectAtIndex:i];
        
        UIImageView *mImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width - 20 , 150 )];
        
        [mImageView setImageWithURL:[NSURL URLWithString:model1.picListThumbUrl] placeholderImage:nil];
        mImageView.contentMode = UIViewContentModeScaleAspectFill;
        mImageView.userInteractionEnabled = YES;
      
        [self.MyScrollView addSubview:mImageView];
        [mImageView release];
    }
    
    self.MyScrollView.contentSize = CGSizeMake(self.frame.size.width  * model.picListarrayData.count, 150);

    [self.contentView bringSubviewToFront:self.chefIcon];
    page = [[UIPageControl alloc] initWithFrame:CGRectMake(100, self.MyScrollView.frame.size.height + 130, 110, 20)];
    page.backgroundColor = [UIColor clearColor];
    page.currentPage = 0;
    page.currentPageIndicatorTintColor = [UIColor blueColor];
    page.pageIndicatorTintColor = [UIColor whiteColor];
    page.numberOfPages = model.picListarrayData.count;
    [self.contentView addSubview:page];
    [page release];

    
    
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger i = scrollView.contentOffset.x / self.frame.size.width ;
    
    page.currentPage = i;

}
- (void)collecting
{
    
    CftModelData *model= [[CftModelData alloc] init];
    model.name = self.nameLabel.text;
    model.desc = self.descLabel.text;
    model.shopName = self.shopNameLabel.text;
    model.favoriteNum = [self.favoriteNumButton titleForState:UIControlStateNormal];
    
    
    BOOL result = [DataBaseManager isExists:model.name];
    if (result) {
        [self.favoriteNumButton setImage:[UIImage imageNamed:@"quxiaoshoucang1"] forState:UIControlStateNormal];
        [DataBaseManager deleteValue:model.name];
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"已取消收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter setAlertViewStyle:UIAlertViewStyleDefault];
        [alter show];
        [alter release];
    }else
    {
        [self.favoriteNumButton setImage:[UIImage imageNamed:@"shoucang1"] forState:UIControlStateNormal];
        [DataBaseManager insertValue:model];
        
        UIAlertView *alter1 = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alter1 show];
        [alter1 release];
    }
    
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
